package com.cls.business.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.utils.MyUtil;
import com.cls.common.entity.MyConstant;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.github.pagehelper.PageInfo;
import com.cls.business.entity.Indicator;
import com.cls.business.service.IIndicatorService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 评审指标表 Controller
 *
 * @author slx
 * @date 2020-11-13 15:14:45
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class IndicatorController extends BaseController {

    private final IIndicatorService indicatorService;

    /**
    * 评审指标表列表页面
    **/
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicator")
    public String indicatorIndex(){
        return MyUtil.view("business/indicator/indicator");
    }

    /**
    * 评审指标表新增页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicator/indicatorAdd")
    public String indicatorAdd(Model model){
        return MyUtil.view("business/indicator/indicatorAdd");
    }

    /**
    * 评审指标表修改页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicator/indicatorUpdate/{indicatorId}")
    public String indicatorUpdate(@PathVariable Long indicatorId, Model model){
        Indicator indicator = indicatorService.getById(indicatorId);
        model.addAttribute("indicator",indicator);
        return MyUtil.view("business/standard/indicator/indicatorUpdate");
    }



    /**
    * 评审指标表详情页面
    * @return
    */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/indicator/indicatorDetail/{indicatorId}")
    public String indicatorDetail(@PathVariable Long indicatorId, Model model){
        Indicator indicator = indicatorService.getById(indicatorId);
        model.addAttribute("indicator",indicator);
        return MyUtil.view("business/indicator/indicatorDetail");
    }

    @GetMapping("indicator")
    @ResponseBody
    @RequiresPermissions("indicator:list")
    public ResponseVO getAllIndicators(Indicator indicator) {
        return ResponseVO.success(indicatorService.findIndicators(indicator));
    }

    @GetMapping("indicator/list")
    @ResponseBody
    public ResponseVO indicatorList(QueryRequest request, Indicator indicator) {
        PageInfo<Indicator> pageInfo =  this.indicatorService.findIndicators(request, indicator);
        return ResponseVO.success(pageInfo);
    }

    @ControllerEndpoint(operation = "新增Indicator", exceptionMessage = "新增Indicator失败")
    @PostMapping("indicator")
    @ResponseBody
    public ResponseVO addIndicator(@Valid Indicator indicator) {
        // 判断权重是否超过100
        Long standardId = indicator.getStandardId();
        Indicator indi=new Indicator();
        indi.setStandardId(standardId);
        List<Indicator> indicatorList = indicatorService.findIndicators(indi);
        Double weightAll =0.0;
        for (Indicator i:indicatorList) {
            weightAll+=i.getWeight();
        }
       if(weightAll>=100){
           return ResponseVO.failure("当前列表权重和已满100！不能新增，请修改后新增");
       }
        Double addWeight=weightAll+indicator.getWeight();
       if(addWeight>100){
           return ResponseVO.failure("当前新增权重过大，请修改信息");
       }
        this.indicatorService.createIndicator(indicator);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除Indicator", exceptionMessage = "删除Indicator失败")
    @PostMapping("indicator/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("indicator:delete")
    public ResponseVO deleteIndicators(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.indicatorService.deleteIndicators(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个Indicator", exceptionMessage = "删除Indicator失败")
    @PostMapping("indicator/delete/{indicatorId}")
    @ResponseBody
    public ResponseVO deleteIndicator(@PathVariable Long indicatorId) {
        this.indicatorService.removeById(indicatorId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Indicator", exceptionMessage = "修改Indicator失败")
    @PostMapping("indicator/update")
    @ResponseBody
    public ResponseVO updateIndicator(Indicator indicator) {
        this.indicatorService.updateIndicator(indicator);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Indicator", exceptionMessage = "导出Excel失败")
    @PostMapping("indicator/excel")
    @ResponseBody
    @RequiresPermissions("indicator:export")
    public void export(QueryRequest queryRequest, Indicator indicator, HttpServletResponse response) {
        List<Indicator> indicators = this.indicatorService.findIndicators(queryRequest, indicator).getList();
        ExcelKit.$Export(Indicator.class, response).downXlsx(indicators, false);
    }
}
